package cate.game.admin.menu.bag;

import cate.common.table.d.GDObj;
import cate.common.table.item.gem.row.GemBaseRow;
import cate.game.GameBody;
import cate.game.admin.menu.MenuSetup;
import cate.game.client.msg.MergeMsgSender;
import cate.game.res.MixResItem;
import cate.game.role.bag.IBagContainer;
import cate.game.role.bag.equip.msg.EquipInitResp;
import cate.game.role.bag.furniture.msg.FurnitureInitResp;
import cate.game.role.bag.gem.Gem;
import cate.game.role.bag.gem.msg.GemInitResp;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.hero.msg.HeroInitResp;
import cate.game.role.bag.item.BaseBagItem;
import cate.game.role.bag.prop.msg.PropInitResp;
import cate.game.role.bag.rune.msg.RuneInitResp;
import cate.game.role.bag.spice.msg.SpiceInitResp;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BagMenu extends MenuSetup {
    final String BAG_TYPE = "游戏对象类型（1：英雄 2：装备 3：道具 4：碎片 5：徽章 6：星格 7:灵器 8:仓库 9觉醒灵器 10 厨具 13：家具 14:宝物 16:载具 19：宝石 20:铭文）";

    public BagMenu(GameBody gameBody) {
        super("背包");
        reg(
                gameBody,
                "获得指定物品",
                3,
                String.format("【参数1】：%s 【参数2】：道具模板ID，【参数3】：数量（默认1个）", BAG_TYPE),
                (role, params) -> {
                    if (params.size() < 2) {
                        gameBody.notice.message(role, "参数不能少于2个!");
                        return;
                    }
                    byte type = Byte.parseByte(params.get(0));
                    int templateId = Integer.parseInt(params.get(1));
                    long num = 1;
                    if (params.size() >= 3 && !StringUtils.isBlank(params.get(2))) {
                        num = Long.parseLong(params.get(2));
                    }
                    EcResult<Void> r = new MixResItem(type, templateId, num).add(role, null);
                    if (r.ok()) {
                        role.getGame().notice.message(role, "恭喜你,添加成功!");
                    } else {
                        role.getGame().notice.message(role, "很遗憾, 添加失败!");
                    }
                });
        reg(
                gameBody,
                "所有英雄各来一朵",
                2,
                "【参数1】星级  【参数2】重复制造",
                (master, params) -> {
                    int star = Integer.parseInt(params.get(0));
                    boolean repeat = toBoolean(params, 1, false);
                    MergeMsgSender sender = new MergeMsgSender();
                    gameBody.table.hero.base.list.forEach(
                            tpl -> {
                                if (tpl.real && tpl.gmProducible) {
                                    if (star > 0 && tpl.star != star) {
                                        return;
                                    }
                                    if (!repeat && master.getBag().hero.count(tpl.id) > 0) {
                                        return;
                                    }
                                    master.getBag().hero.forGmAddCapacity();
                                    master.getBag().hero.addItem(tpl.id, 1, sender);
                                }
                            });
                    sender.send(master, false);
                    gameBody.notice.message(master, "造了许许多多的英雄仔们!");
                });
        reg(
                gameBody,
                "所有装备各来一件",
                0,
                "无视背包容量",
                (master, params) -> {
                    MergeMsgSender sender = new MergeMsgSender();
                    gameBody.table.equip.base.list.forEach(
                            tpl -> {
                                master.getBag().getEquip().forGmAddCapacity();
                                master.getBag().getEquip().addItem(tpl.id, 1, sender);
                            });
                    sender.send(master, true);
                    gameBody.notice.message(master, "装备都送你了!");
                });

        reg(
                gameBody,
                "清空背包",
                1,
                "这个你得慎重啊!【参数1】：" + BAG_TYPE,
                (master, params) -> {
                    if (params.isEmpty()) {
                        gameBody.notice.message(master, "参数不能少于1个!");
                        return;
                    }
                    byte type = Byte.parseByte(params.get(0));
                    IBagContainer<? extends BaseBagItem> bag = master.getBag().getBag(type);
                    if (bag == null) {
                        gameBody.notice.message(master, "不支持的背包对象类型!");
                        return;
                    }
                    bag.getList().clear();

                    master.send(new PropInitResp(master));
                    master.send(new HeroInitResp(master));
                    master.send(new EquipInitResp(master));
                    master.send(new RuneInitResp(master));
                    master.send(new FurnitureInitResp(master));
                    master.send(new SpiceInitResp(master));
                    master.send(new GemInitResp(master));
                    gameBody.notice.message(
                            master,
                            String.format("哎,我给你清空了%s背包！", GDObj.Type.name(type)));
                });

        reg(
                gameBody,
                "清空不存在的英雄",
                0,
                "",
                (master, params) -> {
                    Iterator<Hero> iter = master.getBag().hero.getList().iterator();
                    while (iter.hasNext()) {
                        Hero hero = iter.next();
                        if (hero.getTpl(gameBody) == null) {
                            iter.remove();
                            master.getRes().worldTree.heroShare.delHeroShare(hero.uid);
                        }
                    }
                    master.getRes().worldTree.heroShare.onHeroLvlChange(true);
                    gameBody.notice.message(
                            master, "成功拉！");
                });
        reg(
                gameBody,
                "test万能碎片合成",
                4,
                "【参数1】碎片id  【参数2】英雄id 【参数3】合成数量 【参数4】合成规则",
                (master, params) -> {
                    int id = Integer.parseInt(params.get(0));
                    int tid = Integer.parseInt(params.get(1));
                    int num = Integer.parseInt(params.get(2));
                    int rule = Integer.parseInt(params.get(3));
                    master.getBag().heroPart.chooseFreeComposite(400004, tid, num, rule);
                    gameBody.notice.message(
                            master, "成功拉！");
                });

        reg(
                gameBody,
                "所有宝石来一个",
                1,
                "【参数1】等级",
                (master, params) -> {
                    int level = toInt(params, 0, -1);
                    List<Gem> gems = new ArrayList<>();
                    for (GemBaseRow row : gameBody.table.gem.base.getList()) {
                        if (level > 0 && level != row.level) {
                            continue;
                        }
                        Gem gem = new Gem(master, row.id, 1);
                        gems.add(gem);
                    }
                    master.getBag().gem.addItems(gems, null);
                    gameBody.notice.message(master, "成功");
                });
    }
}
